from flask import Flask, render_template , request
import RPi.GPIO as GPIO
from Motor import Motor
import time

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO_LED = 19
GPIO.setup(GPIO_LED,GPIO.OUT)

M = Motor()

app = Flask(__name__)
a = 'checked'
@app.route('/',methods=['GET','POST'])
def webpi():
    if request.method =='POST':
        a = request.form["on2"]
        b = request.form["radio"]
        if b == 'one':
            if a == 'on':
                M.forward()
                time.sleep(0.2)
                M.stop()
                time.sleep(5)
                M.backward()
                time.sleep(0.5)
                M.stop()
                return render_template('webpi.html',i = 'ON',a = 'checked')
            else:
                M.backward()
                time.sleep(0.5)
                M.stop()
                return render_template('webpi.html',i = 'OFF',a = 'checked')
        if b == 'two':
            if a == 'on':
                GPIO.output(GPIO_LED, True)
                return render_template('webpi.html',j = 'ON',b = 'checked')
            else:
                GPIO.output(GPIO_LED, False)
                return render_template('webpi.html',j = 'OFF',b = 'checked')
        else:
            return render_template('webpi.html',w = 'please choose the botton!')

    else:
        return render_template('webpi.html')
    
if __name__ == "__main__":
    app.run(host='192.168.1.127', port=12315, debug=True)
